using Infrastructure.Attribute;
using Infrastructure.Extensions;
using System.Linq.Expressions;
using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using Mapster;
using ZR.Repository;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model.Enums;

namespace ZR.Service.BaseFrame.Service
{

    /// <summary>
    /// 操作日志服务基类
    /// </summary>
    public class OperLogServiceBase : BaseService<OperLogEntity>
    {





            /// <summary>
            /// 获取操作日志分页列表
            /// </summary>
            /// <param name="req"></param>
            /// <returns></returns>
            public virtual async Task<PagedInfo<OperLogListItemDto>> GetOperLogPageList(GetOperLogPageListReq req)
            {
                    var filterExp=PageListFilterExp(req);
                
                    var result = await Repository.Context.Queryable<OperLogEntity>()
                                .Where(filterExp.ToExpression())
                                .WhereIF(!string.IsNullOrEmpty(req.Keywords), 
                                    m => 
                                        m.Title.Contains(req.Keywords)
                                )
                                .OrderBy(m=>m.OperTime,OrderByType.Desc)
                    
                                .Select(m=>new OperLogListItemDto
                                {
                                            OperId=m.OperId,
                                            Title=m.Title,
                                            BusinessType=m.BusinessType,
                                            Method=m.Method,
                                            RequestMethod=m.RequestMethod,
                                            OperatorType=m.OperatorType,
                                            OperName=m.OperName,
                                            OperUrl=m.OperUrl,
                                            OperIp=m.OperIp,
                                            OperLocation=m.OperLocation,
                                            Status=m.Status,
                                            ErrorMsg=m.ErrorMsg,
                                            OperTime=m.OperTime,
                                            Elapsed=m.Elapsed,
                                })
                    .ToPageAsync(req);
                    return result;
        }

        /// <summary>
        /// 分页列表过滤表达式
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        private static Expressionable<OperLogEntity> PageListFilterExp(GetOperLogPageListReq req)
        {
                var predicate = Expressionable.Create<OperLogEntity>();

                return predicate;
        }

        /// <summary>
        /// 获取操作日志详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<OperLogDetailDto> GetOperLogDetail(long id)
        {
                var detail = await Queryable()
                    .Where(x => x.OperId == id)
                    .FirstAsync();
                var result = detail.Adapt<OperLogDetailDto>();
                return result;
        }

    }
}